package cn.fl.carloan.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.api.ISysUsrQueryApiService;
import cmm.user.facade.system.constant.SysUsrSysRConstant;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysRefUsrDTO;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.carloan.service.IOdrBscInfoService;
import cn.fl.carloan.transform.OdrBscInfoVOTransform;
import cn.fl.carloan.transform.OdrPrdBscInfoVOTransform;
import cn.fl.carloan.transform.OdrQuatPrpsVOTransform;
import cn.fl.carloan.vo.OdrBscInfoVO;
import cn.fl.carloan.vo.OdrPrdBscInfoVO;
import cn.fl.carloan.vo.OdrQuatPrpsVO;
import cn.fl.customer.transform.CstBscInfoVOTransform;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.transform.PrjBscInfoVOTransform;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.system.transform.SysUlFilesRecVOTransform;
import cn.fl.system.vo.SysUlFilesRecVO;

import com.alibaba.fastjson.JSONObject;

import fl.carloan.facade.carloan.api.IOdrBscInfoApiService;
import fl.carloan.facade.carloan.api.IOdrPrdBscInfoApiService;
import fl.carloan.facade.carloan.api.IOdrQuatPrpsApiService;
import fl.carloan.facade.carloan.condition.OdrBscInfoQC;
import fl.carloan.facade.carloan.condition.OdrPrdBscInfoQC;
import fl.carloan.facade.carloan.condition.OdrQuatPrpsQC;
import fl.carloan.facade.carloan.constant.OdrBscInfoConstant;
import fl.carloan.facade.carloan.constant.OdrPrdBscInfoConstant;
import fl.carloan.facade.carloan.dto.OdrBscInfoDTO;
import fl.carloan.facade.carloan.dto.OdrPrdBscInfoDTO;
import fl.carloan.facade.carloan.dto.OdrQuatPrpsDTO;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.dto.CstBscInfoDTO;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.SysFlowSHDTO;


/**
 * @Description: 预约记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class OdrBscInfoServiceImpl implements IOdrBscInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(OdrBscInfoServiceImpl.class);

    @Autowired
    private IOdrBscInfoApiService odrBscInfoApiService;
    @Autowired
    private IOdrPrdBscInfoApiService odrPrdBscInfoApiService;
    @Autowired
    private IOdrQuatPrpsApiService odrQuatPrpsApiService;
    @Autowired
    private ICustomerApiService customerApiService;
    @Autowired
	private IProjectApiService projectApiService;
    @Autowired
    private ISysUsrQueryApiService sysUsrQueryApiService;
    @Autowired
    private ISysUsrApiService sysUsrApiService;
    /**
     * 字典服务层BEAN
     */
    @Autowired
    private ISysOrgApiService sysOrgApiService;
    /**
     * 客户查询服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private IFlowQueryApiService flowQueryApiService;
    //区域维护
    @Autowired
    private ISysRegionApiService sysRegionApiService;
    @Autowired
	private IUploadApiService uploadApiService;

	@Override
	public OdrBscInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		OdrBscInfoDTO dto = this.odrBscInfoApiService.selectById(id);
		OdrBscInfoVO vo = OdrBscInfoVOTransform.toVO(dto);
		//用户信息
		if(StringUtils.isNotEmpty(vo.getUsrId())){
			SysUsrDTO usrDto = sysUsrApiService.selectSysUsrById(vo.getUsrId(),CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			if(usrDto!=null){
				vo.setUsrMp(usrDto.getMp());
			}
		}
		//预约产品信息
		OdrPrdBscInfoQC odrPrdBscInfoQC = new OdrPrdBscInfoQC();
		odrPrdBscInfoQC.setOdrId(id);
		odrPrdBscInfoQC.setIsDel(0);
		List<OdrPrdBscInfoDTO> odrPrdBscInfoDTOList = this.odrPrdBscInfoApiService.selectOdrPrdBscInfoList(odrPrdBscInfoQC);
		if(CollectionUtils.isNotEmpty(odrPrdBscInfoDTOList)){
			vo.setOdrPrdBscInfoVO(OdrPrdBscInfoVOTransform.toVO(odrPrdBscInfoDTOList.get(0)));
		}else{
			vo.setOdrPrdBscInfoVO(new OdrPrdBscInfoVO());
		}
		//配置方案信息
		OdrQuatPrpsQC odrQuatPrpsQC = new OdrQuatPrpsQC();
		odrQuatPrpsQC.setOdrId(id);
		odrQuatPrpsQC.setIsDel(0);
		List<OdrQuatPrpsDTO> odrQuatPrpsDTOList = this.odrQuatPrpsApiService.selectOdrQuatPrpsList(odrQuatPrpsQC);
		if(CollectionUtils.isNotEmpty(odrQuatPrpsDTOList)){
			vo.setOdrQuatPrpsVO(OdrQuatPrpsVOTransform.toVO(odrQuatPrpsDTOList.get(0)));
		}else{
			vo.setOdrQuatPrpsVO(new OdrQuatPrpsVO());
		}
		//客户及配偶信息
		if(StringUtils.isNotEmpty(vo.getCstId())){
			CstBscInfoDTO cstdto = this.customerApiService.selectCstBscInfoDetailById(vo.getCstId(), 
					CurrentThreadContext.getCurrentSysCd(), 
					CurrentThreadContext.getCurrentOrgCd(),
					CurrentThreadContext.getCurrentBuOrgCd());
			if(cstdto!=null){
				vo.setCstBscInfoVO(CstBscInfoVOTransform.toVO(cstdto));
			}else{
				vo.setCstBscInfoVO(new CstBscInfoVO());
			}
		}	
		//推荐人信息
		List<Long> refUsrList=new ArrayList<Long>();
		refUsrList.add(vo.getUsrId());
		List<SysRefUsrDTO> sysRefUsrDTOs = this.sysUsrQueryApiService.searchRefUsrInfoListUp(refUsrList, OdrBscInfoConstant.SYS_CD, SysUsrSysRConstant.REF_USR_BUS_TYP_1);
		if(CollectionUtils.isNotEmpty(sysRefUsrDTOs)){
			SysRefUsrDTO sysRefUsrDTO = sysRefUsrDTOs.get(0);
			if(sysRefUsrDTO.getRefUsrOneId()!=null){				
				vo.setRefOneNm(sysRefUsrDTO.getRefUsrOneRlNm()==null?"未实名":sysRefUsrDTO.getRefUsrOneRlNm());
			}
			if(sysRefUsrDTO.getRefUsrTwoId()!=null){					
				vo.setRefTwoNm(sysRefUsrDTO.getRefUsrTwoRlNm()==null?"未实名":sysRefUsrDTO.getRefUsrTwoRlNm());
			}
		}
		//项目信息
		if(vo.getId()!=null){
			PrjBscInfoVO prjBscInfoVO = PrjBscInfoVOTransform.toVO(this.projectApiService.selectById(vo.getId()));
			if(prjBscInfoVO!=null){				
				vo.setPrjBscInfoVO(prjBscInfoVO);
			}else{
				vo.setPrjBscInfoVO(new PrjBscInfoVO());
			}
		}else{
			vo.setPrjBscInfoVO(new PrjBscInfoVO());
		}
		//增加字典
		this.setDictionaryOption(vo,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return vo;
	}

	@Override
	public Long add(OdrBscInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		//return this.odrBscInfoApiService.add(OdrBscInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除对象主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		//return this.odrBscInfoApiService.removeOdrBscInfoDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(OdrBscInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		return this.odrBscInfoApiService.modifyOdrBscInfoDTO(OdrBscInfoVOTransform.toDTO(entity));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}else{
			//推荐人查询条件转换为用户ID list
			OdrBscInfoQC qc = (OdrBscInfoQC)page.getCondition();
			List<Long> uList=new ArrayList<Long>();
			Map<Long, Long> uMap=new HashMap<Long, Long>();
			if(StringUtils.isNotEmpty(qc.getFirstUsrNm())){
				List<Long> usrIdList=new ArrayList<Long>(1);
				usrIdList.add(Long.valueOf(qc.getFirstUsrNm()));
				List<SysRefUsrDTO> sysRefUsrDTOs = this.sysUsrQueryApiService.searchRefUsrInfoListDown(usrIdList, OdrBscInfoConstant.SYS_CD, SysUsrSysRConstant.REF_USR_BUS_TYP_1);
				if(CollectionUtils.isNotEmpty(sysRefUsrDTOs)){
					for(SysRefUsrDTO dto:sysRefUsrDTOs){
						if(uMap!=null&&!uMap.containsKey(dto.getRefUsrOneId())){
							uList.add(dto.getRefUsrOneId());
							uMap.put(dto.getRefUsrOneId(), dto.getRefUsrOneId());
						}
						
					}
				}
			}
			if(StringUtils.isNotEmpty(qc.getSecondUsrNm())){
				List<Long> usrIdList=new ArrayList<Long>(1);
				usrIdList.add(Long.valueOf(qc.getSecondUsrNm()));
				List<SysRefUsrDTO> sysRefUsrDTOs = this.sysUsrQueryApiService.searchRefUsrInfoListDown(usrIdList, OdrBscInfoConstant.SYS_CD, SysUsrSysRConstant.REF_USR_BUS_TYP_1);
				if(CollectionUtils.isNotEmpty(sysRefUsrDTOs)){
					for(SysRefUsrDTO dto:sysRefUsrDTOs){
						if(uMap!=null&&!uMap.containsKey(dto.getRefUsrTwoId())){
							uList.add(dto.getRefUsrTwoId());
							uMap.put(dto.getRefUsrTwoId(), dto.getRefUsrTwoId());
						}
					}
				}
			}
			if(CollectionUtils.isNotEmpty(uList)){
				qc.setUsrIdList(uList);
				page.setCondition(qc);
			}
		}
		page = this.odrBscInfoApiService.searchOdrAndProjectInfoListPage(page,
					CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd());
		List<OdrBscInfoDTO> odrBscInfoDTOList = (List<OdrBscInfoDTO>)page.getResultList();
		if(CollectionUtils.isNotEmpty(odrBscInfoDTOList)){
			this.setDictionaryOption(odrBscInfoDTOList,CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd());
			this.setOdrBscInfoOrgInfo(odrBscInfoDTOList,CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd());
			List<OdrBscInfoVO> voList=OdrBscInfoVOTransform.toVOList(odrBscInfoDTOList);
			List<Long> usrIdList = new ArrayList<Long>();
			Map<Long, String> ref1Map=new HashMap<>();
			Map<Long, String> ref2Map=new HashMap<>();
			if(CollectionUtils.isNotEmpty(voList)){
				for(OdrBscInfoVO vo:voList){
					usrIdList.add(vo.getUsrId());
				}
				List<SysRefUsrDTO> sysRefUsrDTOs = this.sysUsrQueryApiService.searchRefUsrInfoListUp(usrIdList, OdrBscInfoConstant.SYS_CD, SysUsrSysRConstant.REF_USR_BUS_TYP_1);
				if(CollectionUtils.isNotEmpty(sysRefUsrDTOs)){
					for(SysRefUsrDTO sysRefUsrDTO:sysRefUsrDTOs){
						if(sysRefUsrDTO.getRefUsrOneId()!=null){							
							ref1Map.put(sysRefUsrDTO.getUsrId(), (sysRefUsrDTO.getRefUsrOneRlNm()==null?"未实名":sysRefUsrDTO.getRefUsrOneRlNm())+sysRefUsrDTO.getRefUsrOneId());
						}
						if(sysRefUsrDTO.getRefUsrTwoId()!=null){		
							ref2Map.put(sysRefUsrDTO.getUsrId(), (sysRefUsrDTO.getRefUsrTwoRlNm()==null?"未实名":sysRefUsrDTO.getRefUsrTwoRlNm())+sysRefUsrDTO.getRefUsrTwoId());
						}
					}
				}
				for(OdrBscInfoVO vo:voList){
					if(ref1Map!=null&&ref1Map.containsKey(vo.getUsrId())){
						vo.setRefOneNm(ref1Map.get(vo.getUsrId()));
					}
					if(ref2Map!=null&&ref2Map.containsKey(vo.getUsrId())){
						vo.setRefTwoNm(ref2Map.get(vo.getUsrId()));
					}
				}
			}
			page.setResultList(voList);
		}
		return page;
	}

    
    private void setDictionaryOption(List<OdrBscInfoDTO> list, String sysCd, String orgCd) {
        Map<String, String> prjTypCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_PRJ_TYPE_CD); //项目类型名称
        Map<String, String> bizTypCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_PRJ_BIZ_TYP_CD);//业务类型名称
        Map<String, String> dateResCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, OdrBscInfoConstant.CODE_SYS_DS_CD);//车抵贷数据来源
        Map<String, String> prjStsMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_AUD_STS_CD);//项目状态
        Map<String, SysDictionaryDTO> odrStsCdMap = sysDictionaryApiService.searchGroupToObjMap(sysCd, orgCd, OdrBscInfoConstant.ODR_STS_CD);//预约状态
   
        List<Long> prjidList = new ArrayList<Long>();
        List<Long> cstIdList = new ArrayList<Long>();
        List<Long> usrIdList = new ArrayList<Long>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (OdrBscInfoDTO odrBscInfoDTO : list) {
            	         		
            	prjidList.add(odrBscInfoDTO.getId());
                cstIdList.add(odrBscInfoDTO.getCstId());
                usrIdList.add(odrBscInfoDTO.getUsrId());
                //项目类型
                if (null != odrBscInfoDTO.getPrjTypCd()) {
                    if (prjTypCdMap.containsKey(odrBscInfoDTO.getPrjTypCd())) {
                    	odrBscInfoDTO.setPrjTypCdNm(prjTypCdMap.get(odrBscInfoDTO.getPrjTypCd()));
                    }
                }
                if (null != odrBscInfoDTO.getBizTypCd()) {
                    if (bizTypCdMap.containsKey(odrBscInfoDTO.getBizTypCd())) {
                    	odrBscInfoDTO.setBizTypCdNm(bizTypCdMap.get(odrBscInfoDTO.getBizTypCd()));
                    }
                }
                if (null != odrBscInfoDTO.getDateResCd()) {
                    if (dateResCdMap.containsKey(odrBscInfoDTO.getDateResCd())) {
                        odrBscInfoDTO.setDateResCdNm(dateResCdMap.get(odrBscInfoDTO.getDateResCd()));
                    }
                }
                if (null != odrBscInfoDTO.getOdrStsCd()) {
                    if (odrStsCdMap.containsKey(odrBscInfoDTO.getOdrStsCd())) {
                    	if(StringUtils.isNotEmpty(odrStsCdMap.get(odrBscInfoDTO.getOdrStsCd()).getCodeAttr1())){                    		
                    		odrBscInfoDTO.setOdrStsCdNm(odrStsCdMap.get(odrBscInfoDTO.getOdrStsCd()).getCodeAttr1());
                    	}else if(StringUtils.isNotEmpty(odrStsCdMap.get(odrBscInfoDTO.getOdrStsCd()).getCodeAttr2())){
                    		odrBscInfoDTO.setOdrStsCdNm(odrStsCdMap.get(odrBscInfoDTO.getOdrStsCd()).getCodeAttr2());
                    	}else{
                    		odrBscInfoDTO.setOdrStsCdNm(odrStsCdMap.get(odrBscInfoDTO.getOdrStsCd()).getCodeNm());
                    	}
                    }
                }

            }
            List<String> busTypList = new ArrayList<String>();
            Map<Long, SysFlowSHDTO> flowMap = new HashMap<Long, SysFlowSHDTO>();
            if(CollectionUtils.isNotEmpty(prjidList)){
                SysFlowSHQueryCondition qc = new SysFlowSHQueryCondition();
                qc.setDataIdList(prjidList);
                qc.setWordsInTheTable("PRJ");
                qc.setSysCd(sysCd);
                busTypList.add(FlowConstant.FLOW_MODEL_TYP_PRJ_1);
                qc.setBusTypList(busTypList);
                qc.setStatus(BaseConstant.IS_YESNO_YES);
                List<SysFlowSHDTO> flowSHList = this.flowQueryApiService.searchFlowSHList(qc);
                if (CollectionUtils.isNotEmpty(flowSHList)) {
                    for (SysFlowSHDTO sysFlowSHDTO : flowSHList) {
                        if (!flowMap.containsKey(sysFlowSHDTO.getDataId())) {
                            flowMap.put(sysFlowSHDTO.getDataId(), sysFlowSHDTO);
                        }
                    }
                }
            }
            for (OdrBscInfoDTO odrBscDTO : list) {
                if (null != flowMap && flowMap.containsKey(odrBscDTO.getId())) {
                    SysFlowSHDTO sysFlowSHDTO = flowMap.get(odrBscDTO.getId());
                    odrBscDTO.setPrjStsCd(sysFlowSHDTO.getAudStsCd());
                    if (prjStsMap.containsKey(odrBscDTO.getPrjStsCd())) {
                    	odrBscDTO.setPrjStsCdNm(prjStsMap.get(odrBscDTO.getPrjStsCd()));
                    }
                    odrBscDTO.setpNo(sysFlowSHDTO.getPNo());
                    odrBscDTO.setpInstId(sysFlowSHDTO.getPInstId());
                }
                if (null == odrBscDTO.getPrjStsCd() || odrBscDTO.getPrjStsCd().equals(FlowConstant.FLOW_CHECK_STATE_NON)) {
                	odrBscDTO.setPrjStsCd(FlowConstant.FLOW_CHECK_STATE_NON);
                	odrBscDTO.setPrjStsCdNm(prjStsMap.get(odrBscDTO.getPrjStsCd()));
                }
                
            }

        }

    }
    
    /**
     * 设置组织架构名称
     *
     * @param listDTO
     * @param sysCd
     * @param orgCd
     */
    private void setOdrBscInfoOrgInfo(List<OdrBscInfoDTO> listDTO, String sysCd, String orgCd) {
    	List<SysOrgDTO> list = this.sysOrgApiService.searchSysOrgListBy(CurrentThreadContext.getCurrentSysCd());
        Map<String, SysOrgDTO> buOrgCdMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (SysOrgDTO sysOrgDTO : list) {
                buOrgCdMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO);
            }
        }
        SysOrgDTO sysOrgDTO = null;
    	//设置组织架构名称
        for (OdrBscInfoDTO odrBscInfoDTO : listDTO) {
            if (StringUtils.isEmpty(odrBscInfoDTO.getCstMgrOrgCd())) return;
            String codeNm = "";
            if (null != odrBscInfoDTO.getBuOrgCd()) {
                codeNm = this.sysOrgApiService.selectSysOrgNmByOrgCd(odrBscInfoDTO.getBuOrgCd(), sysCd);
                odrBscInfoDTO.setBuOrgCdNm(codeNm);
            }
            
            if (buOrgCdMap.containsKey(odrBscInfoDTO.getCstMgrOrgCd())) {
	            sysOrgDTO = buOrgCdMap.get(odrBscInfoDTO.getCstMgrOrgCd());
	            odrBscInfoDTO.setCstMgrOrgCdNm(sysOrgDTO.getOrgNm());
	            orgCd = sysOrgDTO.getOrgCd();
	            while(orgCd.length()>8){
	                if("4".equals(sysOrgDTO.getOrgTypCd())){//分公司
	                	odrBscInfoDTO.setCyOrgCdNm(sysOrgDTO.getOrgNm());
	                }else if("3".equals(sysOrgDTO.getOrgTypCd())){
	                	odrBscInfoDTO.setPrOrgCdNm(sysOrgDTO.getOrgNm());
	                    break;
	                }
	                orgCd = orgCd.substring(0,orgCd.length()-4);
	                if(buOrgCdMap.containsKey(orgCd)){
	                    sysOrgDTO = buOrgCdMap.get(orgCd);
	                }else{
	                    break;
	                }
	            }
	        }
        }
        
    }

	@Override
	public List<KeyValueVO> getOdrStsCdList() throws BaseException {
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApiService
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), OdrBscInfoConstant.ODR_STS_CD);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				if(StringUtils.isNotEmpty(sysDictionaryDTO.getCodeAttr1())){
					KeyValueVO keyValue = new KeyValueVO();
					keyValue.setKey(sysDictionaryDTO.getCode());
					keyValue.setValue(sysDictionaryDTO.getCodeAttr1());
					keyValueList.add(keyValue);
				}else if(StringUtils.isNotEmpty(sysDictionaryDTO.getCodeAttr2())){
					KeyValueVO keyValue = new KeyValueVO();
					keyValue.setKey(sysDictionaryDTO.getCode());
					keyValue.setValue(sysDictionaryDTO.getCodeAttr2());
					keyValueList.add(keyValue);
				}else{
					KeyValueVO keyValue = new KeyValueVO();
					keyValue.setKey(sysDictionaryDTO.getCode());
					keyValue.setValue(sysDictionaryDTO.getCodeNm());
					keyValueList.add(keyValue);
				}
				
			}
		}
		return keyValueList;
	}

	@Override
	public List<KeyValueVO> searchBusinessOrgList() throws BaseException {
		List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		if(CollectionUtils.isEmpty(list)){
			return kvList;
		}
		for (SysOrgDTO sysOrgDTO : list) {
			KeyValueVO kv = new KeyValueVO();
			kv.setKey(sysOrgDTO.getOrgCd());
			kv.setValue(sysOrgDTO.getOrgNm());
			kvList.add(kv);
		}
		return kvList;
	}
	
	private void setDictionaryOption(OdrBscInfoVO vo,String sysCd,String orgCd){
		Map<String, String> dateResCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, OdrBscInfoConstant.CODE_SYS_DS_CD);//车抵贷数据来源
		Map<String, String> prdLvlCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, OdrPrdBscInfoConstant.PRD_LVL_CD);//车抵贷车辆等级
		Map<String, String> odrUsrLevelPctMap = this.sysDictionaryApiService.searchGroupToMap(OdrBscInfoConstant.SYS_CD, OdrBscInfoConstant.ORG_CD, OdrBscInfoConstant.ODR_USR_LEVEL_PCT);
        Map<String, String> odrPdMap = this.sysDictionaryApiService.searchGroupToMap(OdrBscInfoConstant.SYS_CD, OdrBscInfoConstant.ORG_CD, OdrBscInfoConstant.ODR_LS_PD_REF_CD);
        Map<String, String> creditRstCdMap = this.sysDictionaryApiService.searchGroupToMap(OdrBscInfoConstant.SYS_CD, OdrBscInfoConstant.ORG_CD, OdrBscInfoConstant.ODR_CREDIT_RST_CD);
		
		if(dateResCdMap.containsKey(vo.getDateResCd())){
			vo.setDateResCdNm(dateResCdMap.get(vo.getDateResCd()));
		}
		if(vo.getOdrPrdBscInfoVO()!=null){
			//车辆等级
			if(vo.getOdrPrdBscInfoVO().getPrdLvl()!=null&&prdLvlCdMap.containsKey(vo.getOdrPrdBscInfoVO().getPrdLvl()+"")){
				vo.getOdrPrdBscInfoVO().setPrdLvlNm(prdLvlCdMap.get(vo.getOdrPrdBscInfoVO().getPrdLvl()+""));
			}
			//省名称
			if(vo.getOdrPrdBscInfoVO().getRgnPrCd()!=null){
				SysRegionDTO prRegion = this.sysRegionApiService.getRegionByRgnCd(vo.getOdrPrdBscInfoVO().getRgnPrCd(), sysCd);
				if(prRegion!=null){
					vo.getOdrPrdBscInfoVO().setRgnPrCdNm(prRegion.getRgnNm());
				}
			}
			//市名称
			if(vo.getOdrPrdBscInfoVO().getRgnCyCd()!=null){
				SysRegionDTO cyRegion = this.sysRegionApiService.getRegionByRgnCd(vo.getOdrPrdBscInfoVO().getRgnCyCd(), sysCd);
				if(cyRegion!=null){
					vo.getOdrPrdBscInfoVO().setRgnCyCdNm(cyRegion.getRgnNm());
				}
			}
			//是否有发票原件
			if(vo.getOdrPrdBscInfoVO()!=null&&vo.getOdrPrdBscInfoVO().getIsHaveIvc()!=null){
				if(vo.getOdrPrdBscInfoVO().getIsHaveIvc().toString().equals(OdrPrdBscInfoConstant.IS_HAVE_IVC_YES)){
					vo.getOdrPrdBscInfoVO().setIsHaveIvcNm(OdrPrdBscInfoConstant.IS_HAVE_IVC_YES_NM);
				}else if(vo.getOdrPrdBscInfoVO().getIsHaveIvc().toString().equals(OdrPrdBscInfoConstant.IS_HAVE_IVC_NO)){
					vo.getOdrPrdBscInfoVO().setIsHaveIvcNm(OdrPrdBscInfoConstant.IS_HAVE_IVC_NO_NM);
				}
			}
			//一级推荐人
			if(vo.getOdrQuatPrpsVO()!=null&&vo.getRefOneNm()!=null){
				Double usrPct=Double.valueOf(odrUsrLevelPctMap.get(OdrBscInfoConstant.ODR_USR_LEVEL_1));
				Double pdPct =null;
				if(odrPdMap.containsKey(vo.getOdrQuatPrpsVO().getLsPd()+"")){
					pdPct=Double.valueOf(odrPdMap.get(vo.getOdrQuatPrpsVO().getLsPd()+""));
				}
				if(usrPct==null){						
					usrPct=0D;
				}
				if(pdPct==null){						
					pdPct=0D;
				}
				vo.setRefOneAmtNm(Double.valueOf(DecimalUtils.formatScientificNotatio(vo.getOdrQuatPrpsVO().getContAmt()*usrPct*pdPct, 0, false))/100+"");
			}
			//二级推荐人
			if(vo.getOdrQuatPrpsVO()!=null&&vo.getRefTwoNm()!=null){
				Double usrPct=Double.valueOf(odrUsrLevelPctMap.get(OdrBscInfoConstant.ODR_USR_LEVEL_2));
				Double pdPct =null;
				if(odrPdMap.containsKey(vo.getOdrQuatPrpsVO().getLsPd()+"")){
					pdPct=Double.valueOf(odrPdMap.get(vo.getOdrQuatPrpsVO().getLsPd()+""));
				}
				if(usrPct==null){						
					usrPct=0D;
				}
				if(pdPct==null){						
					pdPct=0D;
				}
				vo.setRefTwoAmtNm(Double.valueOf(DecimalUtils.formatScientificNotatio(vo.getOdrQuatPrpsVO().getContAmt()*usrPct*pdPct, 0, false))/100+"");
			}
			//征信结果
			if(vo.getCreditRstCd()!=null){
				if(creditRstCdMap.containsKey(vo.getCreditRstCd())){
					vo.setCreditRstCdNm(creditRstCdMap.get(vo.getCreditRstCd()));
				}
			}
		}
	}

	@Override
	public List<SysUlFilesRecVO> searchSysUlFilesRecList(
			SysUlFilesRecQueryCondition condition) throws BaseException {
		if(null==condition || StringUtils.isBlank(condition.getSysCd())){
			BaseLogUtils.info(logger, "searchSysUlFilesRecList", "查询所有文件上传记录失败,系统代码为空.DATA:"+JSONObject.toJSONString(condition), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<SysUlFilesRecDTO> list = this.uploadApiService.searchSysUlFilesRecList(condition);
		//这里判断如果没有http开头的文件路径则把IP路径赋值
		if(CollectionUtils.isNotEmpty(list)){
			String serverPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_SERVER_PATH);
			for (SysUlFilesRecDTO sysUlFilesRecDTO : list) {
				String path = sysUlFilesRecDTO.getFilePath();
				if(StringUtils.isEmpty(path) || path.startsWith("http") || StringUtils.isEmpty(serverPath))continue;
				sysUlFilesRecDTO.setFilePath(serverPath + path);
			}
		}
		return SysUlFilesRecVOTransform.toVOList(list);
	}
}
